package com.rest.demo.functional.concurrent;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.*;

/**
 * @author Administrator
 * @decription
 * @Date 2018-01-30 10:53
 */
public class CallableDemo {

    public static void main(String[] args) throws ExecutionException, InterruptedException, TimeoutException {
        Callable<Integer> task = () -> {
            try {
                TimeUnit.SECONDS.sleep(5);
                return 123;
            } catch (InterruptedException e) {
                throw new IllegalStateException("task interrupted",e);
            }
        };

        List<Callable<String>> callableList = Arrays.asList(
                () -> "task1",
                () -> "task2",
                () -> "task3"
        );
        ExecutorService executor = Executors.newWorkStealingPool();
        executor.invokeAll(callableList).stream()
                .map(future -> {
                    String result = "";
                    try {
                        result = future.get();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } catch (ExecutionException e) {
                        e.printStackTrace();
                    }
                    return result;
                })
                .forEach(System.out::println);
    }
}
